<template>
  <div class="flex flex_column">
    <el-dialog
      append-to-body
      title="生产退料"
      :visible.sync="isVisible"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="80%"
      @close="closeMarDialog"
      @open="opens"
    >
      <div class="dialogDody marDialog flex flex_column">
        <div class="btnCon flex flex_between">
          <div>
            <el-button
              slot="append"
              :loading="marLoading"
              type="primary"
              size="small"
              icon="el-icon-plus"
              :disabled="marLoading"
              element-loading-spinner="el-icon-loading"
              @click="newAddClick"
            >
              新建
            </el-button>
          </div>
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="materalSearch.likeWords"
              placeholder="输入关键字"
              size="small"
            >
              <el-button
                slot="append"
                :loading="marLoading"
                type="primary"
                icon="el-icon-search"
                :disabled="marLoading"
                element-loading-spinner="el-icon-loading"
                @click="MarterdialogShow"
              >
                搜索
              </el-button>
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon">
          <el-table :data="dataList" border style="width: 100%">
            <el-table-column
                  prop="backMaterialBillNo"
                  label="退料单号"
                  width="180"
                />
                <el-table-column
                  label="退料类型"
                  prop="backMaterialTypeName"
                  show-overflow-tooltip
                  min-width="100"
                />
                <el-table-column
                  label="单据状态"
                  prop="backMaterialBillStateName"
                  show-overflow-tooltip
                  min-width="100"
                />
                <el-table-column
                  label="入库状态"
                  prop="inStockStateName"
                  min-width="100"
                />
                <el-table-column
                  label="关联单号"
                  prop="produceTaskBillNo"
                  min-width="150"
                />
                <el-table-column
                  label="明细项数"
                  prop="backMaterialKindCount"
                  show-overflow-tooltip
                  min-width="90"
                >
                </el-table-column>
                <el-table-column
                  label="总数量"
                  prop="sumQuantity"
                  min-width="80"
                />
                <el-table-column
                  label="创建时间"
                  prop="createDate"
                  min-width="150"
                />
                <el-table-column
                  label="创建人员"
                  prop="createPersonName"
                  min-width="80"
                />
                <el-table-column
                  label="备注"
                  show-overflow-tooltip
                  min-width="150"
                  prop="backMaterialRemark"
                >
                </el-table-column>
                <el-table-column
          label="操作"
          width="70px"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              <el-popover
                :ref="`popover-${scope.$index}`"
                placement="bottom-end"
                popper-class="tableOp"
                width="160"
                :popper-options="{
                    boundariesElement: 'opBox',
                    removeOnDestroy: true,
                  }"
                trigger="click"
              >
                <div class="operation">
                  <ul>
                    <li @click="handleClick(scope.row)">
                      查看
                    </li>
                    <li v-show="scope.row.backMaterialBillState === 0" @click="handleUpdata(scope.row)">
                      修改
                    </li>
                  </ul>
                  <ul>
                    <li v-show="scope.row.backMaterialBillState === 0" @click="handleConfirm(scope.row)">
                      确认
                    </li>
                    <li v-show="scope.row.backMaterialBillState === 1 && scope.row.inStockState === 0 " @click="handleDone(scope.row)">
                      作废
                    </li>
                    <li v-show="scope.row.backMaterialBillState === 2 " @click="handleCopy(scope.row)">
                      复制
                    </li>
                  </ul>
                  <ul>
                    <li v-show="scope.row.backMaterialBillState === 0" @click="handleDelete(scope.row)">
                      删除
                    </li>
                  </ul>
                </div>
                <div slot="reference" class="opBox pointer">
                  <span class="el-icon-more c_3F61F1" />
                </div>
              </el-popover>
            </div>
          </template>
        </el-table-column>
          </el-table>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
      </div>

      <!-- 人员寻找弹框   -->
      <per-redio-dialog
        :per-visible.sync="isperShow"
        @getEmployee="getEmployee"
      />
      <el-dialog
        :title="tname"
        :visible.sync="isAddProduct"
        :close-on-click-modal="false"
        custom-class="bodyNoPadDialog bigDialog"
        width="85%"
        append-to-body
        @close="closeDialog"
        @open="openCp"
      >
        <div class="dialogDody">
          <div class="subTip">
            <span class="mr15"
              ><span class="mr5">入库单号：</span
              >{{ ProductForm.backMaterialBillNo || "系统自动生成" }}</span
            >
            <span class="mr15"
              ><span class="mr5">创建日期：</span
              >{{
                ProductForm.createDate || dayjs().format("YYYY-MM-DD")
              }}</span
            >
            <span
              ><span class="mr5">创建人员：</span
              >{{ opsid != 1 ? ProductForm.createPersonName : name }}</span
            >
          </div>
          <div class="addWuliaoBox">
            <el-form
              ref="ProductForm"
              :model="ProductForm"
              :rules="rules"
              label-width="110px"
              label-position="left"
              class="dialogForm dialogInlineForm"
              :disabled="canDiable"
            >
              <el-row>
                <el-col :span="8">
                  <el-form-item label="退料类型" prop="backMaterialTypeName">
                    <el-input
                      v-model="ProductForm.backMaterialTypeName"
                      placeholder="生产退料"
                      size="mini"
                      disabled
                    />
                    <!-- <el-select
                        filterable v-model="ProductForm.getMaterialType" placeholder="领料类型" size="mini">
                    <el-option
                      v-for="item in purchaseTypes"
                      :key="item.dicValueID"
                      :label="item.dicValueDesc"
                      :value="item.dicValueCode"
                    />
                  </el-select> -->
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="关联单据">
                    <div class="flex flex_center">
                      <div class="flex1">
                        <el-input
                          v-model="ProductForm.getMaterialBillNo"
                          placeholder="选择生产单"
                          size="mini"
                          disabled
                        />
                      </div>
                      <div class="ml10">
                        <selectProductOrder />
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="退料人员" prop="backMaterialPersonName">
                    <div class="flex flex_center">
                      <div class="flex1">
                        <el-input
                          v-model="ProductForm.backMaterialPersonName"
                          size="mini"
                          placeholder="请选择人员"
                        >
                          <el-button
                            slot="append"
                            icon="el-icon-user"
                            @click="isperShow = true"
                          />
                        </el-input>
                      </div></div
                  ></el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="退料机台" prop="equipmentNo">
                    <el-input
                      v-model="ProductForm.equipmentName"
                      placeholder="退料设备"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="客户单号" prop="customerBillNo">
                    <el-input
                      v-model="ProductForm.customerBillNo"
                      placeholder="客户单号"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="产品名称" prop="orderProductName">
                    <el-input
                      v-model="ProductForm.orderProductName"
                      placeholder="产品名称"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="版本版面" prop="productVersion">
                    <el-input
                      v-model="ProductForm.productVersion"
                      placeholder="版本版面"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="生产单号" prop="produceTaskBillNo">
                    <el-input
                      v-model="ProductForm.produceTaskBillNo"
                      placeholder="生产单号"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="8">
                  <el-form-item label="产品编码" prop="productCode">
                    <el-input
                      v-model="ProductForm.productCode"
                      placeholder="产品编码"
                      size="mini"
                      disabled
                    />
                  </el-form-item>
                </el-col> -->
              </el-row>
              <el-form-item label="退料备注" class="inlineText">
                <el-input
                  v-model="ProductForm.backMaterialRemark"
                  type="textarea"
                  :rows="1"
                  placeholder="退料备注"
                  size="mini"
                  clearable
                />
              </el-form-item>
            </el-form>
            <div class="tableBox">
              <div class="tt flex flex_between">
                <span class="fw">退料明细</span>
                <span
                  v-if="!canDiable"
                  class="c_3F61F1 mr15 pointer"
                  @click="isSemidialog = true"
                  ><i class="el-icon-plus mr5" /> 新增</span
                >
              </div>
              <div ref="bigdialogTable" class="bigdialogTbCon">
                <el-table
                  :data="ProductForm.mxDataList"
                  border
                  style="width: 100%"
                >
                  <el-table-column
                    prop="stockGoodsCode"
                    label="物料编号"
                    width="120"
                  />
                  <el-table-column
                    label="物料类型"
                    prop="goodsCategoryName"
                    show-overflow-tooltip
                    min-width="100"
                  />
                  <el-table-column
                    label="供应商"
                    prop="supplierName"
                    show-overflow-tooltip
                    min-width="100"
                  />
                  <el-table-column
                    label="物料名称"
                    prop="goodsName"
                    min-width="100"
                  />
                  <el-table-column
                    label="规格型号"
                    prop="stockGoodsName"
                    min-width="100"
                  />
                  <el-table-column
                    label="领料数量"
                    prop="receiveQuantity"
                    min-width="100"
                  />
                  <el-table-column
                    label="退料数量"
                    prop="backQuantity"
                    show-overflow-tooltip
                    min-width="90"
                  >
                    <template slot-scope="{ row }">
                      <el-input
                        v-model="row.backQuantity"
                        :disabled="canDiable"
                        placeholder="请输入"
                        size="mini"
                        onkeyup="value=value.replace(/[^\d]/g,'')"
                        @input="backQuantityInput(row)"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="单位"
                    prop="goodsUnitName"
                    min-width="80"
                  />
                  <el-table-column
                    label="备注"
                    show-overflow-tooltip
                    min-width="150"
                    prop="backRemark"
                  >
                    <template slot-scope="{ row }">
                      <el-input
                        v-model="row.backRemark"
                        :disabled="canDiable"
                        placeholder="请输入"
                        size="mini"
                      />
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-if="!canDiable"
                    label="操作"
                    fixed="right"
                    show-overflow-tooltip
                    min-width="80"
                    align="center"
                  >
                    <template slot-scope="scope">
                      <el-tooltip
                        class="item"
                        effect="dark"
                        content="删除"
                        placement="right-start"
                      >
                        <span
                          class="pointer"
                          @click="purchaseDelete(scope.$index)"
                          ><i class="el-icon-delete"
                        /></span>
                      </el-tooltip>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="isAddProduct = false">取 消</el-button>
          <!-- 新增       -->
          <el-button
            v-if="opsid !== 2"
            :loading="addProductLoading"
            :disabled="addProductLoading"
            element-loading-spinner="el-icon-loading"
            type="primary"
            size="mini"
            @click="savell"
            >{{ btm }}</el-button
          >
        </div>
      </el-dialog>

      <!-- 原因   -->
    <el-dialog
      title="作废原因"
      :visible.sync="isNopassshow"
      :close-on-click-modal="false"
      custom-class="comDialog"
      width="40%"
      @close="closePassDialog"
      append-to-body
    >
      <div class="dialogDody">
        <el-form ref="passForm" :model="passForm" class="dialogForm">
          <el-form-item class="mt12">
            <el-input
              v-model="passForm.cancelReason"
              prop="cancelReason"
              type="textarea"
              placeholder="请输入内容"
              :autosize="{ minRows: 6 }"
              size="mini"
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isNopassshow = false">取 消</el-button>
        <el-button
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="passSave"
        >提交</el-button>
      </span>
    </el-dialog>

      <el-dialog
        append-to-body
        title="选择领料单"
        :visible.sync="isPickingShow"
        :close-on-click-modal="false"
        custom-class="bodyNoPadDialog bigDialog"
        width="70%"
        @close="closeSemiDialog"
      >
        <div class="dialogDody marDialog flex flex_column">
          <div class="btnCon flex flex_end">
            <!--      搜索框-->
            <div class="search">
              <el-input
                v-model="SemiSearch.likeWords"
                placeholder="输入关键字"
                size="small"
              >
                <el-button
                  slot="append"
                  :loading="marLoading"
                  type="primary"
                  icon="el-icon-search"
                  :disabled="marLoading"
                  element-loading-spinner="el-icon-loading"
                  @click="MarterdialogShow"
                >
                  搜索
                </el-button>
              </el-input>
            </div>
          </div>
          <!-- 表格区域       -->
          <div class="tableCon">
            <el-table
              :data="pickingDataList"
              border
              style="width: 100%"
              @selection-change="handlepickingChange"
            >
              <el-table-column prop="selection" width="55">
                <template slot-scope="scope">
                  <el-radio
                    v-model="radio"
                    :label="scope.$index"
                    @change.native="getCurrentRow(scope.row)"
                    >{{
                  }}</el-radio>
                </template>
              </el-table-column>
              <el-table-column prop="getMaterialBillNo" label="领料单号" />
              <el-table-column prop="getMaterialTypeName" label="领料类型" />
              <el-table-column prop="createDate" label="创建时间" />
              <el-table-column prop="equipmentName" label="领料机台" />
              <el-table-column prop="getMaterialPersonName" label="领料人员" />
              <el-table-column prop="getMaterialKindCount" label="领料明细" />
              <el-table-column prop="produceTaskBillNo" label="生产单号" />
              <el-table-column prop="xieYiShuHao" label="订单单号" />
              <el-table-column prop="customerBillNo" label="客户单号" />
              <!-- <el-table-column prop="productCode" label="订单产品编码" /> -->
              <el-table-column prop="orderProductName" label="订单产品名称" />
              <el-table-column prop="sumQuantity" label="总数量" />
              <el-table-column
                width="105"
                prop="createPersonName"
                label="创建人员"
              />
              <el-table-column
                width="105"
                prop="getMaterialRemark"
                label="备注"
              />
            </el-table>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="isPickingShow = false">取 消</el-button>
          <!-- 选择订单 -->
          <el-button
            v-loading="addLoading"
            :disabled="addLoading"
            element-loading-spinner="el-icon-loading"
            type="primary"
            size="mini"
            @click="savePicking"
          >
            确定</el-button
          >
        </div>
      </el-dialog>

      <el-dialog
        append-to-body
        title="选择物料"
        :visible.sync="isSemidialog"
        :close-on-click-modal="false"
        custom-class="bodyNoPadDialog bigDialog"
        width="70%"
        @close="closeSemiDialog"
        @open="openmater"
      >
        <div class="dialogDody marDialog flex flex_column">
          <div class="btnCon flex flex_end">
            <!--      搜索框-->
            <div class="search">
              <el-input
                v-model="SemiSearch.likeWords"
                placeholder="输入关键字"
                size="small"
              >
                <el-button
                  slot="append"
                  :loading="marLoading"
                  type="primary"
                  icon="el-icon-search"
                  :disabled="marLoading"
                  element-loading-spinner="el-icon-loading"
                  @click="openmater"
                >
                  搜索
                </el-button>
              </el-input>
            </div>
          </div>
          <!-- 表格区域       -->
          <div class="tableCon">
            <el-table
              :data="materialList"
              border
              style="width: 100%"
              @selection-change="handleSelectionChange"
            >
              <el-table-column prop="selection" width="55">
                <el-table-column type="selection" width="55" />
                <el-table-column prop="stockGoodsCode" label="物料编号" />
                <el-table-column prop="goodsName" label="物料名称" />
                <el-table-column prop="stockGoodsName" label="规格型号" />
                <el-table-column prop="supplierName" label="供应商" />
                <el-table-column prop="goodsCategoryName" label="物料类型" />
                <el-table-column prop="receiveQuantity" label="领料数量" />
                <el-table-column prop="goodsUnitName" label="单位" />
                <el-table-column
                  prop="goodsGroupName"
                  label="所属分组"
                /> </el-table-column
            ></el-table>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="isSemidialog = false">取 消</el-button>
          <!-- 选择订单 -->
          <el-button
            v-loading="addLoading"
            :disabled="addLoading"
            element-loading-spinner="el-icon-loading"
            type="primary"
            size="mini"
            @click="saveColorManuscript"
          >
            确定</el-button
          >
        </div>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
import SelectSCorder from "@/components/selectSCorder";
import perRedioDialog from "@/components/perDialogRedio";
import selectProductOrder from "@/components/selectProductOrder";
import SelectMateriaList from "@/components/selectMaterial";
import { mapGetters } from "vuex";
import { string } from "yargs";
export default {
  name: "returnMaterial",
  props: {
    perVisible: {
      type: Boolean,
      required: false,
    },
    equipmentNo: {
      type: String,
      required: true,
    },
    produceTaskBillNo: {
      type: String,
      required: true,
    },
  },
  components: {
    SelectSCorder,
    perRedioDialog,
    SelectMateriaList,
    selectProductOrder,
  },
  data() {
    return {
      ops: "add", // 操作、新建，修改，查看
      opsid: 1, // 3 确定 1 新建， 4 修改， 2 查看
      tableHeight: null, // 表格最大高度
      showOrder: false,
      isSeeMaterriel: false, // 查看弹框是否打开
      isAddMaterriel: false, // 新建弹框是否打开
      addMaterrielLoading: false, // 新建保存加载
      searchLoading: false, // 搜索加载效果
      allMaterirls: [], // 全部数据列表
      colorState: null,
      addLoading: false,
      selectRow: null,
      isNopassshow: false,
      equipmentList: [],
      passForm: {
        cancelReason: "",
      },
      // 表格操作
      ProductForm: {
        backMaterialType: 1,
        backMaterialPersonName: "",
        equipmentNo: "",
        equipmentName: "",
        mxDataList: [],
      },
      // 搜索条件
      search: {
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
      },
      total: 0, // 总条数
      tableHead: [], // 表格表头
      addProductLoading: false,
      isAddProduct: false,
      isperShow: false,
      radio: "",
      isSemidialog: false,
      dataList: [],
      pickingDataList: [], //领料单数据
      isPickingShow: false, //领料单弹出层显示
      multipleSelection: null,
      purchaseInfoDatas: [{}],
      allSupliers: [],
      templateSelection: null,
      purchaseTypes: [
        {
          dicValueID: 0,
          dicValueDesc: "生产领料",
          dicValueCode: 1,
        },
        {
          dicValueID: 1,
          dicValueDesc: "生产补料",
          dicValueCode: 2,
        },
      ],
      materalSearch: {
        machineCode: "", //生产机台代码
        produceTaskBillNo: "", //生产任务单号
        likeWords: "",
      },
      SemiSearch: [],
      materialList: [],
      marLoading: false,
      canDiable: false,
      rules: {
        backMaterialPersonName: [
          { required: true, message: "请选择人员", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["name", "userInfo"]),
    btm() {
      if (this.opsid === 3) {
        return "确认";
      } else {
        return "保存";
      }
    },
    tname() {
      if (this.opsid === 1) {
        return "新建生产退料单";
      } else if (this.opsid === 2) {
        return "查看生产退料单";
      } else if (this.opsid === 3) {
        return "确定生产退料单";
      } else if (this.opsid === 4) {
        return "修改生产退料单";
      } else {
        return "复制生产退料单";
      }
    },
    isVisible: {
      get() {
        return this.perVisible;
      },
      set(val) {
        this.$emit("update:perVisible", val);
      },
    },
  },
  watch: {
    perVisible(o, n) {
      if (o) {
        this.MarterdialogShow();
      }
    },
  },
  updated() {},
  created() {},
  mounted() {},
  methods: {
    opens() {
      this.materalSearch.likeWords = "";
      // this.MarterdialogShow()
    },
    closeMarDialog() {
      this.isVisible = false;
    },
    MarterdialogShow() {
      this.materalSearch.machineCode = this.equipmentNo;
      this.materalSearch.produceTaskBillNo = this.produceTaskBillNo;
      const parmas = this.materalSearch;
      this.marLoading = true;
      this.$api.product
        .GetMachinesProduceBackMaterialBillList(parmas)
        .then((res) => {
          // const _p = [];
          // res.forEach((e) => {
          //   _p.push(e.mxDataList);
          // });
          this.dataList =res;
          console.log(this.dataList)
          this.marLoading = false;
        })
        .catch(() => {});
    },

    closePassDialog() {
      this.passForm.cancelReason = "";
      this.selectRow = null;
    },

    // 新建点击事件
    newAddClick() {
      this.ops = "add";
      // this.isAddProduct = true
      this.opsid = 1;
      this.materalSearch.machineCode = this.equipmentNo;
      this.materalSearch.produceTaskBillNo = this.produceTaskBillNo;
      const parmas = this.materalSearch;
      this.marLoading = true;
      this.$api.product
        .GetMachinesProduceGetMaterialBills(parmas)
        .then((res) => {
          const _p = [];
          res.forEach((e) => {
            _p.push(e.baseData);
          });
          this.pickingDataList = _p;
          //   this.pickingDataList = res;
          //   console.log(this.pickingDataList)
          this.isPickingShow = true;
          this.marLoading = false;
        })
        .catch(() => {});
      //   this.showOrder = true
        this.canDiable = false
      //   this.ProductForm.backMaterialPersonName = this.userInfo.employeeName
      //   this.ProductForm.backMaterialPerson = this.userInfo.employeeCode
    },

    handlepickingChange() {},
    getCurrentRow(row) {
      // 获取选中数据   row表示选中这一行的数据，可以从里面提取所需要的值
      this.templateSelection = row;
    },

    savePicking() {
      let row = this.templateSelection;
      if (row) {
        this.ops = "add";
        this.isAddProduct = true;
        this.ProductForm.getMaterialBillNo = row.getMaterialBillNo;
        this.ProductForm.backMaterialTypeName = "生产退料";
        this.ProductForm.equipmentName = row.equipmentName;
        this.ProductForm.produceTaskBillNo = row.produceTaskBillNo;
        this.ProductForm.saleOrderNo = row.saleOrderNo;
        this.ProductForm.productVersion =
          row.printingSurface + row.productVersion;
        this.ProductForm.productCode = row.productCode;
        this.ProductForm.customerBillNo = row.customerBillNo;
        this.ProductForm.productCode = row.productCode;
        this.ProductForm.orderProductName = row.orderProductName;
        this.ProductForm.backMaterialPersonName = this.userInfo.employeeName;
        this.ProductForm.backMaterialPerson = this.userInfo.employeeCode;
        this.isPickingShow = false;
      } else {
        this.$message({
          message: "请选择领料单",
          type: "error",
        });
      }
    },

    backQuantityInput(row) {
      if (Number(row.backQuantity) > Number(row.applyGetQuantity)) {
        this.$message.warning("退料数量不能大于领料数量");
        row.backQuantity = row.applyGetQuantity;
      }
    },
    selectCurrentRow(row) {
      console.log(row);
      this.ops = "add";
      this.isAddProduct = true;
      this.ProductForm.getMaterialBillNo = row.getMaterialBillNo;
      this.ProductForm.backMaterialTypeName = "生产退料";
      this.ProductForm.equipmentName = row.equipmentName;
      this.ProductForm.produceTaskBillNo = row.produceTaskBillNo;
      this.ProductForm.saleOrderNo = row.saleOrderNo;
      this.ProductForm.productVersion =
        row.printingSurface + row.productVersion;
      this.ProductForm.productCode = row.productCode;
      this.ProductForm.customerBillNo = row.customerBillNo;
      this.ProductForm.productCode = row.productCode;
      this.ProductForm.orderProductName = row.orderProductName;
    },
    purchaseDelete(index) {
      this.ProductForm.mxDataList.splice(index, 1);
    },

    handleClick(row) {
      this.opsid = 2
      this.canDiable = true
      this.getDetail(row)
    },
    handleConfirm(row) {
      this.opsid = 3
      this.canDiable = true
      this.getDetail(row)
    },
    handleDone(row) {
      this.selectRow = row
      this.isNopassshow = true
    },
    handleCopy(row) {
      this.opsid = 5
      this.canDiable = false
      // this.getDetail(row)
      this.openCp()

      const _data = {
        backMaterialID: row.backMaterialID,
        backMaterialBillNo: row.backMaterialBillNo
      }
      this.$api.product.GetBackProduceMaterialBillDetail(_data).then((res) => {
        this.equipmentList.push({
          equipmentName: res.baseData.equipmentName,
          equipmentNo: res.baseData.equipmentNo
        })
        delete res.baseData.backMaterialID
        delete res.baseData.backMaterialBillNo
        this.ProductForm = res.baseData
        this.ProductForm.mxDataList = res.mxDataList
        this.isAddProduct = true
      })
    },
    handleUpdata(row) {
      this.opsid = 4
      this.canDiable = false
      this.getDetail(row)
    },

    getDetail(row) {
      const _data = {
        backMaterialID: row.backMaterialID,
        backMaterialBillNo: row.backMaterialBillNo
      }
      this.$api.product.GetBackProduceMaterialBillDetail(_data).then((res) => {
        this.equipmentList.push({
          equipmentName: res.baseData.equipmentName,
          equipmentNo: res.baseData.equipmentNo
        })
        this.ProductForm = res.baseData
        this.ProductForm.productVersion = res.baseData.printingSurface+res.baseData.productVersion
        this.ProductForm.mxDataList = res.mxDataList
        this.isAddProduct = true
      })
    },
    backQuantityInput(row){
      if(Number(row.backQuantity)>Number(row.applyGetQuantity)){
        this.$message.warning('退料数量不能大于领料数量')
        row.backQuantity = row.applyGetQuantity
      }
    },
    passSave() {
      const _data = {
        backMaterialID: this.selectRow.backMaterialID,
        cancelReason: this.passForm.cancelReason
      }
      this.$api.product.CancelBackProduceMaterialBill(_data).then(() => {
        this.isNopassshow = false
        this.addLoading = false
        this.$message({
          message: '作废成功',
          type: 'success'
        })
      }).catch(() => {
      })
    },
    handleDelete(row) {
      const _data = {
        backMaterialID: row.backMaterialID
      }
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.product.DeleteBackProduceMaterialBill(_data).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
            this.MarterdialogShow()
        }).catch(() => {
        })
      })
    },

    // 选择物料
    openmater() {
      const _data = {
        getMaterialBillNo: this.ProductForm.getMaterialBillNo,
        likeWords: "",
      };
      this.$api.product.GetGoodsByGetMaterialBillNo(_data).then((res) => {
        // res.forEach(v => {
        //   v.goodsCategoryName = v.categoryName
        //   v.planUseQuantity = v.planMaterialQuantity
        // })
        this.materialList = res;
      });
    },
    openCp() {
      const _data = {
        getMaterialBillNo: this.ProductForm.getMaterialBillNo,
        likeWords: "",
      };
      this.$api.product.GetProduceTaskEquipmentList(_data).then((res) => {
        this.equipmentList = res;
        if (res && res.length) {
          this.ProductForm.equipmentNo = res[0].equipmentNo;
        }
      });
    },
    savell() {
      this.ProductForm.equipmentNo = this.equipmentNo
      if (this.opsid === 3) {
        const _data3 = {
          backMaterialID: this.ProductForm.backMaterialID,
        };
        this.$api.product
          .SureBackProduceMaterialBill(_data3)
          .then(() => {
            this.addProductLoading = false;
            this.isAddProduct = false;
            this.MarterdialogShow();
            this.$message({
              message: "确认成功",
              type: "success",
            });
          })
          .catch(() => {
            this.addProductLoading = false;
          });
      } else if (this.opsid === 4) {
        const parmas = this.ProductForm;
        this.$refs.ProductForm.validate((valid) => {
          if (valid) {
            this.addProductLoading = true;
            this.$api.product
              .UpdateBackProduceMaterialBill(parmas)
              .then(() => {
                this.addProductLoading = false;
                this.isAddProduct = false;
                this.MarterdialogShow();
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addProductLoading = false;
              });
          }
        });
      } else {
        const parmas = this.ProductForm;
        this.$refs.ProductForm.validate((valid) => {
          if (valid) {
            this.addProductLoading = true;
            this.$api.product
              .CreateBackProduceMaterialBill(parmas)
              .then(() => {
                this.addProductLoading = false;
                this.isAddProduct = false;
                this.MarterdialogShow();
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
              })
              .catch(() => {
                this.addProductLoading = false;
              });
          }
        });
      }
    },

    // 得到申请人员
    getEmployee(val) {
      this.ProductForm.backMaterialPersonName = val.employeeName;
      this.ProductForm.getMaterialPerson = val.employeeCode;
    },
    supplierChange(val) {
      this.materSearch.supplierCode = val;
      this.purchaseInfoDatas = [];
    },
    closeSemiDialog() {
      this.isSemidialog = false;
    },
    handleSelectionChange(val) {
      // val.forEach(res => {
      //   res.goodsCategoryName = res.categoryName
      //   res.planUseQuantity = res.planMaterialQuantity
      // })
      this.multipleSelection = val;
    },
    saveColorManuscript() {
      const array = JSON.parse(JSON.stringify(this.ProductForm.mxDataList));
      const arr = JSON.parse(JSON.stringify(this.multipleSelection));
      arr.forEach((item) => {
        item.backQuantity = 0;
        item.backRemark = "";
      });
      this.ProductForm.mxDataList = arr.reduce((result, item) => {
        const val = result.some(
          ({ stockGoodsCode }) => stockGoodsCode === item.stockGoodsCode
        );
        if (!val) {
          result.push(item);
        }
        return result;
      }, array);
      this.closeSemiDialog();
    },
    // 详情
    getMaterielInfo(id) {},
    // 保存表头样式变化
    saveHead(newArray) {
      const parmas = newArray;
      setTimeout(() => {
        this.$api.common.saveTableStyle(parmas).then(() => {});
      }, 300);
    },
    // 保存表头列宽度
    surverWidth(newWidth, oldWidth, column) {
      setTimeout(() => {
        const heads = [...this.tableHead];
        heads.forEach((item) => {
          if (item.columnCode === column.property) {
            item.columnWidth = column.width;
            return;
          }
        });
        this.saveHead(heads);
      }, 100);
    },
    // 表格最大高度
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.tableCon.getBoundingClientRect();
        this.tableHeight = rect.height;
      });
    },
    // ，清除所有查询条件
    Refresh() {
      this.search = this.$options.data().search;
      this.MarterdialogShow();
    },
    // 获取表格表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.producebackmaterialbilllist,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableHead = res;
      });
    },
    // 新建弹框关闭事件
    closeDialog() {
      if (this.$refs.ProductForm) {
        this.$refs.ProductForm.resetFields();
      }
      this.ProductForm = this.$options.data().ProductForm;
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  height: 100%;
}
.permangeRg {
  border: 1px solid #e6e6e6;
  background: #fff;
  height: 100%;

  .mainTT {
    font-size: 16px;
    font-weight: bold;
    width: 100%;
    padding: 18px 16px;
    padding-bottom: 0;
  }
}
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
</style>
